//
//  ItemsVC.m
//  LessoDemo
//
//  Created by decade on 27/12/2017.
//  Copyright © 2017 Lesso. All rights reserved.
//

#import "ItemsVC.h"
#import "ItemCell.h"
#import "SimilarVC.h"

@interface ItemsVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) PopView *popview;

@end

@implementation ItemsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = @[
                       @{@"title": @"Aluminum", @"img": @"1", @"price": @"US $197.68"},
                       @{@"title": @"Pastoral Style", @"img": @"2", @"price": @"US $97.68"},
                       @{@"title": @"Balcony garden", @"img": @"3", @"price": @"US $68"},
                       ];
    [self setupTableView];
}

- (void)setupTableView {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    UILabel *footLB = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    footLB.numberOfLines = 0;
    footLB.text = @"1.long click cell for 1 second it will show the popView and add to Viewed history. \n\n2.share function need the platform support so it's not implement";
    self.tableView.tableFooterView = footLB;
    
    UILongPressGestureRecognizer *lpress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    lpress.minimumPressDuration = 1;
    [self.tableView addGestureRecognizer:lpress];
}

- (void)longPress:(UILongPressGestureRecognizer *)gestureRecognizer {
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan){
        CGPoint location = [gestureRecognizer locationInView:self.tableView];
        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
        NSInteger rowNum = [indexPath row];
        NSArray *ViewArray = [[NSUserDefaults standardUserDefaults]objectForKey:ViewedHistory];
        NSMutableArray *tmparr = [[NSMutableArray alloc]initWithArray: ViewArray];
        [tmparr addObject:self.dataArray[rowNum]];
        [[NSUserDefaults standardUserDefaults]setObject:tmparr forKey:ViewedHistory];
        [UIView animateWithDuration:0.3 animations:^{
            self.popview = [[PopView alloc]initWithY:rowNum*100+50];
            self.popview.alpha = 1;
            [self.tableView addSubview:self.popview];
            __weak __typeof(self)weakSelf = self;
            self.popview.similarBlock = ^{
                SimilarVC *vc = [[SimilarVC alloc]init];
                [weakSelf.navigationController pushViewController:vc animated:YES];
            };
        } completion:^(BOOL finished) {
            
        }];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cellIdentifier";
    ItemCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[ItemCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier data:self.dataArray[indexPath.row]];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

@end
